leaderboardApp.controller("asistenciasCtrl", function($scope, $http, $timeout, notifier, httpPost, $timeout) {
	$scope.loading = false;
	$scope.message = {title: "", text: "", type: "", visible: false};
	$scope.modalMessage = {title: "", text: "", type: "", visible: false};
	
	$scope.editAsistMode = false;
	
	$scope.asistencias = [];
	$scope.mesesAsistencias = [];
	$scope.mesSeleccionado = {};
	$scope.userNames = [];
	$scope.branches = [];
	$scope.currentBranch = "";
	
	$scope.addAsistData = {asistId: 0, userName: "", month: {}, days: 0, branch: {}, permanent: false};
	
	$scope.getMesesAsistencias = function() {
		$scope.loading = true;
		httpPost.send($scope.message, "/Leaderboard/AsistenciasServlet/getMesesAsistencias", {}, function(data) {
			if(data.code === 0) {
				$scope.mesesAsistencias = data.meses;
				if($scope.mesesAsistencias.length > 0) {
					$scope.mesSeleccionado = $scope.mesesAsistencias[0];
					$scope.getAsistencias();
				}
				if($("#cmbMeses,#cmbSucursal,#cmbAddEditMes").select2 != null) {
					$("#cmbMeses,#cmbSucursal,#cmbAddEditMes").select2();
					$timeout(function() { $("#cmbMeses,#cmbSucursal,#cmbAddEditMes").trigger("change"); }, 0, false);
				}
			}
			$scope.loading = false;
		});
	};
	$scope.getMesesAsistencias();
	
	$scope.getAsistencias = function() {
		$scope.loading = true;
		httpPost.send($scope.message, "/Leaderboard/AsistenciasServlet/getAsistencias", {"anio": $scope.mesSeleccionado.year, "mes": $scope.mesSeleccionado.month}, function(data) {
			if(data.code === 0) {
				$scope.asistencias = data.asistencias;
			}
			$scope.loading = false;
		});
	};
	
	$scope.getUserNames = function() {
		httpPost.send($scope.message, "/Leaderboard/AdminServlet/getUserNames", {}, function(data) {
			if(data.code === 0) {
				$scope.userNames = data.userNames;
				$("#txtUsuario").typeahead({source: $scope.userNames, minChars: 3, max: 10});
			}
		});
	};
	$scope.getUserNames();
	
	$scope.getBranches = function() {
		$scope.loading = true;
		httpPost.send($scope.message, "/Leaderboard/AdminServlet/getBranches", {showAllSelector: "YES", showOwnedOnly: "N"}, function(data) {
			if(data.code === 0) {
				$scope.branches = data.branches;
			}
			$scope.loading = false;
		});
	};
	$scope.getBranches();
	
	$scope.addAsistencia = function() {
		$scope.editAsistMode = false;
		$scope.addAsistData = {asistId: 0, userName: "", month: {}, days: 0, branch: {}, permanent: false};
		
		$("#cmbMeses,#cmbSucursal,#cmbAddEditMes").select2();
		$timeout(function() { $("#cmbMeses,#cmbSucursal,#cmbAddEditMes").trigger("change"); }, 0, false);
		
		$("#dlgAddEditAsist").modal("show");
	};
	
	$scope.editAsistencia = function(asist) {
		$scope.editAsistMode = true;
		$scope.addAsistData = {asistId: 0, userName: "", month: {}, days: 0, branch: {}, permanent: false};
		$scope.addAsistData.asistId = asist.id;
		$scope.addAsistData.userName = asist.user;
		$scope.addAsistData.days = asist.days;
		
		for(var i=0; i<$scope.mesesAsistencias.length; i++) {
			if($scope.mesesAsistencias[i].month === asist.month) {
				$scope.addAsistData.month = $scope.mesesAsistencias[i];
				break;
			}
		}
		
		for(var i=0; i<$scope.branches.length; i++) {
			if($scope.branches[i].id === asist.branchId) {
				$scope.addAsistData.branch = $scope.branches[i];
				break;
			}
		}
		$timeout(function() { $("#cmbMeses,#cmbSucursal,#cmbAddEditMes").trigger("change"); }, 0, false);
		
		$("#dlgAddEditAsist").modal("show");
	};
	
	$scope.openAsistDialog = function(opt, asist) {
		if(opt.indexOf("add") >= 0) {
			$scope.addAsistencia();
		} else {
			$scope.editAsistencia(asist);
		}
	};
	
	$scope.saveAsistencia = function() {
		$scope.loading = true;
		if($scope.editAsistMode) {
			httpPost.send($scope.modalMessage, "/Leaderboard/AsistenciasServlet/updateAsistencia", {asistId: $scope.addAsistData.asistId, "month": $scope.addAsistData.month.month, "year": $scope.addAsistData.month.year, "days": $scope.addAsistData.days, "branchId": $scope.addAsistData.branch.id}, function(data) {
				if(data.code === 0) {
					$("#dlgAddEditAsist").modal("hide");
					notifier.notifySuccess($scope.message, "Ok", data.reason);
					$scope.addAsistData = {asistId: 0, userName: "", month: {}, days: 0, branch: {}, permanent: false};
					$scope.getAsistencias();
				}
				$scope.loading = false;
			});
		} else {
			var userName = $scope.addAsistData.userName;
			userName = userName.substring(0, userName.indexOf(" - "));
			httpPost.send($scope.modalMessage, "/Leaderboard/AsistenciasServlet/addAsistencia", {"userName": userName, "month": $scope.addAsistData.month.month, "year": $scope.addAsistData.month.year, "days": $scope.addAsistData.days, "branchId": $scope.addAsistData.branch.id, "permanent": $scope.addAsistData.permanent? "S": "N"}, function(data) {
				if(data.code === 0) {
					$("#dlgAddEditAsist").modal("hide");
					notifier.notifySuccess($scope.message, "Ok", data.reason);
					$scope.addAsistData = {asistId: 0, userName: "", month: {}, days: 0, branch: {}};
					$scope.getAsistencias();
				}
				$scope.loading = false;
			});
		}
	};
	
	$scope.delAsistencia = function(asistId) {
		if(confirm("Esta seguro que desea eliminar la asistencia con ID " + asistId + "?")) {
			$scope.loading = true;
			httpPost.send($scope.message, "/Leaderboard/AsistenciasServlet/delAsistencia", {"asistId": asistId}, function(data) {
				if(data.code === 0) {
					notifier.notifySuccess($scope.message, "Ok", data.reason);
					$scope.getAsistencias();
				}
				$scope.loading = false;
			});
		}
	};
	
	$scope.getCurrentBranchUser = function() {
		if($scope.addAsistData.userName != null) {
			if($scope.addAsistData.userName.trim().length > 0) {
				var userName = $scope.addAsistData.userName;
				userName = userName.substring(0, userName.indexOf(" - "));
				
				httpPost.send($scope.modalMessage, "/Leaderboard/AdminServlet/getBranchUser", {"user": userName}, function(data) {
					if(data.code === 0) {
						$scope.currentBranch = data.branch;
					}
				});
			}
		}
	};
	
	/************************************** Funciones de Utiler�a **************************************/
	
	$scope.hideMessage = function() {
		notifier.hideNotif($scope.message);
		notifier.hideNotif($scope.modalMessage);
	};
	
	/************************************** ##################### **************************************/
});